/**
 * Copyright (c) 2009 blurredVision
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 
 * OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#ifndef __CONFIG_H__
#define __CONFIG_H__

#pragma once

#include <string>
#include <map>
#include <sstream>

#include "ScriptingEngineLua.h"

extern "C"{
#include "lua.h"
}

/**
 * \ingroup CoreEngine
 * 
 * The Config class.<br>
 * Reads and writes an entire configuration file.<br>
 * Provides access to values stores inside the config file.
 * The Config is stored as a LUA script.
 *
 * \date 03-28-2007
 *
 * \author juckel
 * \author dennis
 *
 */
class DLL_EXPORT Config
//	: public Singleton<Config>		// Defines a singleton object. 
{
public:
	Config(void);
	virtual ~Config(void);

	void Destroy();

	/// Get a number-value from the config file and cast it to class T
	template <class T>
	T getNumber(const std::string &key) const;

	/// Returns an entry as a string value
	std::string getString(const std::string &entry) const;

	/// Returns an entry as a boolean value
	bool getBoolean(const std::string &entry) const;

	/// Create a new number entry or set an old one to another value
	template <class T>
	void setNumber(const std::string &entry, const T &value);

	/// Create a new string entry or set an old one to another value 
	void setString(const std::string &entry, const std::string &value);
	
	/// Sets an bool values for a key entry
	void setBoolean(const std::string &entry, bool value);

	/// Checks, if an entry exists
	bool isDefined(const std::string &entry) const; 

	/// Read the config values from a file
	void read(const std::string &filename);

	/// Writes the config into a file
	void write(const std::string &filename = "");

private:
	/// Trims whitespaces from a string
	void trim(std::string &s);

	/// Storage space for the values
	std::map<std::string, std::string> contents;

	std::string sFilename;		///< The filename of the config file
	lua_State* stateName;		///< The name of the lua state
};

template <class T>
T Config::getNumber(const std::string &key) const
{
	T result = (T) coreGetScriptingEngineLua()->getFieldNum(stateName, "config", key.c_str());
	return result;
}

template <class T>
void Config::setNumber(const std::string &entry, const T &value)
{
	coreGetScriptingEngineLua()->setFieldNum(stateName, "config", entry.c_str(), (lua_Number) value);
}


/**
 *  Returns a reference to the config object
 *  \return The single instance of the config object
 */
DLL_EXPORT 
Config* coreGetConfig();

DLL_EXPORT
bool coreInitConfig();

#endif
